<?php
/**
* @file $Id: ProperDate.fnc.php 162 2006-09-07 11:17:12Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

/*
Outputs a pretty date when sent an oracle or postgres date.
*/

function ProperDate($date='',$length='long')
{	global $_FOCUS;

	if($length=='short')
	{
		if($_FOCUS['Preferences']['Preferences'])
			$old_prefs = $_FOCUS['Preferences']['Preferences'];
		$_FOCUS['Preferences']['Preferences']['WEEKDAY'][1]['VALUE'] = '';
		$_FOCUS['Preferences']['Preferences']['MONTH'][1]['VALUE'] = 'n';
		$_FOCUS['Preferences']['Preferences']['DAY'][1]['VALUE'] = 'j';
		$_FOCUS['Preferences']['Preferences']['YEAR'][1]['VALUE'] = '';
	}

	$months_number['JAN'] = '1';
	$months_number['FEB'] = '2';
	$months_number['MAR'] = '3';
	$months_number['APR'] = '4';
	$months_number['MAY'] = '5';
	$months_number['JUN'] = '6';
	$months_number['JUL'] = '7';
	$months_number['AUG'] = '8';
	$months_number['SEP'] = '9';
	$months_number['OCT'] = '10';
	$months_number['NOV'] = '11';
	$months_number['DEC'] = '12';
		
	if($date && strlen($date)==9)
	{
		$year = substr($date,7);
		$year = (($year<50)?20:19).$year;
		$month = $months_number[strtoupper(substr($date,3,3))];
		$day = substr($date,0,2)*1;
		$comment = '<!-- '.(($year<50)?20:19).$year.MonthNWSwitch(substr($date,3,3),'tonum').(substr($date,0,2)).' -->';
	}
	elseif($date)
	{
		$year = substr($date,0,4);
		$month = substr($date,5,2)*1;
		$day = substr($date,8)*1;
		$comment = '<!-- '.$year.substr($date,5,2).(substr($date,8)).' -->';
	}
	
	if((Preferences('MONTH')=='m' || Preferences('MONTH')=='n') && (Preferences('DAY')=='j' || Preferences('DAY')=='d'))
		$sep = '/';
	else
	{
		$sep = ' ';
		$comma = ',';
	}

	if($date)
	{
		if(Preferences('MONTH_DAY')=='mdy')
		{
			if($year>1970)
				$return = $comment.date(Preferences('WEEKDAY').' '.(($length=='long' || Preferences('MONTH')!='F')?Preferences('MONTH'):'M').$sep.Preferences('DAY').(Preferences('YEAR')?$comma.$sep.Preferences('YEAR'):''),mktime(0,0,0,$month,$day,$year));
			else
				$return = $comment.date(Preferences('WEEKDAY').' '.(($length=='long' || Preferences('MONTH')!='F')?Preferences('MONTH'):'M').$sep.Preferences('DAY'),mktime(0,0,0,$month,$day,'2005')).$comma.$sep.$year;
		}
		else
		{
			if($year>1970)
				$return = $comment.date(Preferences('WEEKDAY').' '.Preferences('DAY').$sep.(($length=='long' || Preferences('MONTH')!='F')?Preferences('MONTH'):'M').(Preferences('YEAR')?$comma.$sep.Preferences('YEAR'):''),mktime(0,0,0,$month,$day,$year));
			else
				$return = $comment.date(Preferences('WEEKDAY').' '.Preferences('DAY').$sep.(($length=='long' || Preferences('MONTH')!='F')?Preferences('MONTH'):'M'),mktime(0,0,0,$month,$day,'2005')).$comma.$sep.$year;
		}
	}

	if($length=='short')
		$_FOCUS['Preferences']['Preferences'] = $old_prefs;

	return $return;
}

function ShortDate($date='',$column='')
{
	return ProperDate($date,'short');
}
?>